function [ Data, Dates ] = gethdata( code, Start, End, Sec)
%    该函数通过载入yahoo的开放数据来读取历史数据
%   输入代码code和Sec,可获取Yahoo的历史数据
%   注意起始日期Start和终止日期End的格式'yyyy-mm-dd'
%    example1:[HistoryPrice,Dates] = gethdata('000001','2015-03-12','2015-04-10','sz')
%	上述是获取sz000001的2015/03/12到2015/04/20的历史价格数据
%	example2:[HistoryPrice] = gethdata('600000','2014-09-01')
%	上述是获取ss600000的2014/09/01至今的数据
%   格式分别对应: 'Open';'High';'Low';'Close';'Volume';'Adj Close'和'Dates'

%% 读取Yahoo的股票Csv数据

if nargin<4
    if strmatch('600',code);
        Sec = 'ss';%600为上交所
    end
    if strmatch('000',code);
        Sec='sz';%000为深交所
    end
end

url = strcat('http://table.finance.yahoo.com/table.csv?s=',code,'.',Sec);
csvdata = urlread(url);

%% 数据提取

[M(:,1) M(:,2) M(:,3) M(:,4) M(:,5) M(:,6) M(:,7)] = strread ...
                      (csvdata, '%s %s %s %s %s %s %s', 'delimiter', ',');
    Dates = M(2:end,1); %去除掉抬头，分割出dates
	Data = M(2:end,2:end);  %去除掉抬头，分割出data

%% 提取时间数据

if nargin==1;
	return;
end


if nargin==2
    if weekday(Start)==1|weekday(Start)==7
        fprintf('\t错误：起始日休市\n');
        return;
    end
    Start = find(strcmp(M(:,1),Start)==1);
    Dates = Dates(1:Start-1,:);
	Data = Data(1:Start-1,:);
end

if nargin==3
    if weekday(Start)==1|weekday(Start)==7
        fprintf('\t错误：起始日休市\n');
        return;
    end
    if weekday(End)==1|weekday(End)==7
        fprintf('\t错误：终止日休市\n');
        return;
    end
    Start = find(strcmp(M(:,1),Start)==1);
    End = find(strcmp(M(:,1),End)==1);
    Dates = Dates(End:Start-1,:);
	Data = Data(End:Start-1,:);
end

%% 数据样式标准化
Data = flipud(Data);
Dates = flipud(Dates);
Data = cellfun(@(x)str2num(x),Data);

end